package com.sugar.follow.up.module.business.service.publicityeducationcls;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.follow.up.module.business.controller.admin.publicityeducationcls.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.publicityeducationcls.PublicityEducationClsDO;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;

import com.sugar.follow.up.module.business.dal.mysql.publicityeducationcls.PublicityEducationClsMapper;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.*;

/**
 * 宣教分类 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class PublicityEducationClsServiceImpl implements PublicityEducationClsService {

    @Resource
    private PublicityEducationClsMapper publicityEducationClsMapper;

    @Override
    public Long createPublicityEducationCls(PublicityEducationClsSaveReqVO createReqVO) {
        // 插入
        PublicityEducationClsDO publicityEducationCls = BeanUtils.toBean(createReqVO, PublicityEducationClsDO.class);
        publicityEducationClsMapper.insert(publicityEducationCls);

        // 返回
        return publicityEducationCls.getId();
    }

    @Override
    public void updatePublicityEducationCls(PublicityEducationClsSaveReqVO updateReqVO) {
        // 校验存在
        validatePublicityEducationClsExists(updateReqVO.getId());
        // 更新
        PublicityEducationClsDO updateObj = BeanUtils.toBean(updateReqVO, PublicityEducationClsDO.class);
        publicityEducationClsMapper.updateById(updateObj);
    }

    @Override
    public void deletePublicityEducationCls(Long id) {
        // 校验存在
        validatePublicityEducationClsExists(id);
        // 删除
        publicityEducationClsMapper.deleteById(id);
    }

    @Override
        public void deletePublicityEducationClsListByIds(List<Long> ids) {
        // 删除
        publicityEducationClsMapper.deleteByIds(ids);
        }


    private void validatePublicityEducationClsExists(Long id) {
        if (publicityEducationClsMapper.selectById(id) == null) {
            throw exception(PUBLICITY_EDUCATION_CLS_NOT_EXISTS);
        }
    }

    @Override
    public PublicityEducationClsDO getPublicityEducationCls(Long id) {
        return publicityEducationClsMapper.selectById(id);
    }

    @Override
    public PageResult<PublicityEducationClsDO> getPublicityEducationClsPage(PublicityEducationClsPageReqVO pageReqVO) {
        return publicityEducationClsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PublicityEducationClsRespVO> getPublicityEducationClsAll() {
        return BeanUtils.toBean(publicityEducationClsMapper.selectList(), PublicityEducationClsRespVO.class);
    }

}